package zhoug.zui.list;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import zhoug.common.adapters.recycler2.BaseRecyclerViewAdapter;
import zhoug.common.adapters.recycler2.BaseViewHolder;
import zhoug.common.utils.AttributeUtils;
import zhoug.zui.R;

/**
 * @Author: zhoug
 * @Date: 2024-07-12
 * @Description:
 */
public abstract class CheckListAdapter<T> extends BaseRecyclerViewAdapter<T> implements SelectListAdapter<T> {
    public static final int RADIO_BUTTON = 1;
    public static final int CHECK_BOX = 2;
    public static final int TEXT = 3;

    /**
     * 布局样式
     * {@link #RADIO_BUTTON,#CHECK_BOX,#TEXT}
     */
    protected int mLayoutType = CheckListAdapter.TEXT;

    /**
     * 布局样式为{@link #RADIO_BUTTON,#CHECK_BOX}时的button图标
     * 为null时使用默认图标
     */
    protected @DrawableRes int mIconRes;
    protected @ColorRes int mIconTintRes;
    /**
     * 图标大小
     */
    protected int mIconSize;
    /**
     * 文字颜色
     */
    protected int mTextColor;
    /**
     * 文字大小
     */
    protected float mTextSize;
    /**
     * v文字样式
     */
    protected int mTextStyle;
    /**
     * 设置每个文字 maxLines
     */
    protected int mTextMaxLines;

    protected boolean mAutoIconRes = true;
    protected boolean mAutoIconTintRes = true;

    private final ListSelectHelper<T> mHelper;

    public abstract String getItemName(T itemData);


    public CheckListAdapter(Context context) {
        super(context);
        mHelper = new ListSelectHelper<>(this);
        mHelper.showSelectImage(false);
    }

    /**
     * 设置布局样式
     *
     * @param layoutType {@link #RADIO_BUTTON,#CHECK_BOX,#TEXT}
     */
    public void setLayoutType(int layoutType) {
        this.mLayoutType = layoutType;
        showSelectImage(mLayoutType != TEXT);
        setCancelSelectEnabled(mLayoutType != RADIO_BUTTON);
        if (mAutoIconRes) {
            if (mLayoutType == RADIO_BUTTON) {
                mIconRes = R.drawable.zui_radio_button;
            } else if (mLayoutType == CHECK_BOX) {
                mIconRes = R.drawable.zui_check_box;
            }
        }
        if (mAutoIconTintRes) {
            if (mLayoutType == RADIO_BUTTON || mLayoutType == CHECK_BOX) {
                mIconTintRes = R.color.zui_drawable_tint;
            }
        }
    }

    /**
     * 布局样式为{@link #RADIO_BUTTON,#CHECK_BOX}时的button图标
     * 为null时使用默认图标
     */
    public void setIconRes(@DrawableRes int iconRes) {
        this.mIconRes = iconRes;
        mAutoIconRes = mIconRes == 0;
    }

    public void setIconTintRes(int iconTintRes) {
        this.mIconTintRes = iconTintRes;
        mAutoIconTintRes = mIconTintRes == 0;
    }

    public void setAutoIconTintRes(boolean autoIconTintRes) {
        this.mAutoIconTintRes = autoIconTintRes;
    }

    public void setIconSize(int iconSize) {
        this.mIconSize = iconSize;
    }

    public void setTextColor(int textColor) {
        this.mTextColor = textColor;
    }

    public void setTextSize(float textSize) {
        this.mTextSize = textSize;
    }

    public void setTextStyle(int textStyle) {
        this.mTextStyle = textStyle;
    }

    public void setTextMaxLines(int textMaxLines) {
        this.mTextMaxLines = textMaxLines;
    }


    @Override
    protected int getLayoutId(int viewType) {
        if(mLayoutType==RADIO_BUTTON || mLayoutType== CHECK_BOX){
            return R.layout.zui_check_list_item_has_icon;
        }
        return R.layout.zui_check_list_item_no_icon;
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        BaseViewHolder holder = super.onCreateViewHolder(parent, viewType);
        TextView tv_selectName = holder.getView(R.id.zui_check_Text);
        ImageView iv_selected = holder.getView(R.id.zui_check_icon);
        AttributeUtils.setMaxLines(tv_selectName, mTextMaxLines);
        AttributeUtils.setTypeface(tv_selectName, mTextStyle);
        AttributeUtils.setTextColor(tv_selectName, mTextColor, 0);
        AttributeUtils.setTextSize(tv_selectName, mTextSize, 0);
        if (mIconSize != 0) {
            AttributeUtils.setSize(iv_selected, mIconSize, mIconSize);
        }

        if(mLayoutType==TEXT){
            if(parent instanceof RecyclerView recyclerView){
                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
               if(layoutManager instanceof GridLayoutManager ||
                layoutManager instanceof StaggeredGridLayoutManager){
                   ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
                   if(layoutParams!=null){
                       layoutParams.width=  ViewGroup.LayoutParams.MATCH_PARENT;
                       holder.itemView.setLayoutParams(layoutParams);
                   }
               }
            }
        }
        return holder;

    }

    @Override
    protected void onBindData(BaseViewHolder holder, T data, int position, int viewType) {
        if (data != null) {
            TextView tv_selectName = holder.getView(R.id.zui_check_Text);
            ImageView iv_selected = holder.getView(R.id.zui_check_icon);
            tv_selectName.setText(getItemName(data));
            mHelper.bindData(data, holder.itemView, tv_selectName, null);
            if(iv_selected!=null){
                Drawable drawable = AttributeUtils.getDrawable(getContext(), mIconRes, mIconTintRes);
                if (drawable != null) {
                    iv_selected.setImageDrawable(drawable);
                }
                if (drawable != null) {
                    iv_selected.setImageDrawable(drawable);
                }
            }

        }
    }

    @Override
    public void notifyDataChange() {
        notifyDataChange(false);
    }

    @Override
    public void notifyDataChange(boolean callSelectChange) {
        notifyDataSetChanged1();
        if (callSelectChange) {
            mHelper.onSelectChange();
        }
    }


    public void showSelectImage(boolean show) {
        mHelper.showSelectImage(show);
    }

    public void setTextAlign(int textAlign) {
        mHelper.setTextAlign(textAlign);
    }

    @Override
    public void setSelect(List<T> data) {
        mHelper.setSelect(data);

    }


    @Override
    public void clearSelect() {
        mHelper.clearSelect();
    }

    @Nullable
    @Override
    public List<T> getSelect() {
        return mHelper.getSelect();
    }

    @Nullable
    @Override
    public  T getSingleSelect() {
        return mHelper.getSingleSelect();
    }

    @Override
    public void setSelect(T data) {
        mHelper.setSelect(data);
    }

    @Override
    public void addSelect(List<T> data) {
        mHelper.addSelect(data);
    }

    @Override
    public void addSelect(T data) {
        mHelper.addSelect(data);
    }

    @Override
    public void clearSelect(T data) {
        mHelper.clearSelect(data);
    }

    @Override
    public void setMultiSelect(boolean multi) {
        mHelper.setMultiSelect(multi);
    }

    @Override
    public boolean isSelect(T data) {
        return mHelper.isSelect(data);
    }

    @Override
    public boolean isMultiSelect() {
        return mHelper.isMultiSelect();
    }

    @Override
    public void setCancelSelectEnabled(boolean enabled) {
        mHelper.setCancelSelectEnabled(enabled);
    }

    @Override
    public void setEnabled(boolean enabled) {
        mHelper.setEnabled(enabled);
    }

    @Override
    public void setOnSelectChangeListener(OnSelectChangeListener<T> onSelectChangeListener) {
        mHelper.setOnSelectChangeListener(onSelectChangeListener);
    }


}
